package io.gitee.yaoshanli.engine.service.impl;

import io.gitee.yaoshanli.engine.db.JwDbHelper;
import io.gitee.yaoshanli.engine.model.WfLinkModel;
import io.gitee.yaoshanli.engine.service.IFlowService;
import io.gitee.yaoshanli.engine.model.WfCodeModel;
import io.gitee.yaoshanli.engine.model.WfNodeModel;

import java.util.ArrayList;
import java.util.List;

public class FlowService implements IFlowService {

    private CodeService codeService=new CodeService();
    @Override
    public boolean checkFlow(WfCodeModel wfCodeModel, String idOrCode) {
        return codeService.checkFlow(wfCodeModel,idOrCode);
    }

    @Override
    public WfCodeModel getWfCode(String idOrCode) {
        return codeService.getWfCode(idOrCode);
    }

    @Override
    public boolean removeAndTask(String idOrCode) {
        return false;
    }

    @Override
    public List<WfCodeModel> getWfCodePage() {
        return null;
    }

    @Override
    public WfNodeModel getFirstNode(String wf_id) {
        String sql="select t1.* from jw_wf_node t1,jw_wf_link t2 where t1.wf_id=? and t1.wf_id=t2.wf_id " +
                " AND t1.id not in (select target_id from jw_wf_link  where wf_id=?) and t2.source_id=t1.id ";
        Object[] args = new Object[]{wf_id,wf_id};
        return JwDbHelper.queryFirst(WfNodeModel.class,sql,args);
    }

    @Override
    public WfNodeModel getNextNodeByTask(String wf_id, String task_id) {
        String sql="select t1.* from jw_wf_node t1,jw_wf_link t2,jw_wf_task t3 where t1.wf_id=? and t1.wf_id=t2.wf_id " +
                " and t3.wf_id=t1.wf_id and t3.node_id=t2.source_id and t3.id=? and t1.id=t2.target_id ";
        Object[] args = new Object[]{wf_id,task_id};
        return JwDbHelper.queryFirst(WfNodeModel.class,sql,args);
    }

    @Override
    public WfNodeModel getNextNode(String wf_id, String node_id) {
        String sql="select t1.* from jw_wf_node t1,jw_wf_link t2 where t1.wf_id=? and t1.wf_id=t2.wf_id " +
                " and t2.source_id=? and t1.id=t2.target_id ";
        Object[] args = new Object[]{wf_id,node_id};
        return JwDbHelper.queryFirst(WfNodeModel.class,sql,args);
    }

    @Override
    public List<WfNodeModel> getNextNodes(String wf_id, String node_id) {
        String sql="select t1.* from jw_wf_node t1,jw_wf_link t2 where t1.wf_id=? and t1.wf_id=t2.wf_id " +
                " and t2.source_id=? and t1.id=t2.target_id ";
        Object[] args = new Object[]{wf_id,node_id};
        List<WfNodeModel> list = JwDbHelper.queryList(WfNodeModel.class,sql,args);
        return list;
    }

    @Override
    public WfNodeModel getNode(String wf_id,String node_id) {
        String sql="select t1.* from jw_wf_node t1 where t1.wf_id=? and t1.id=? ";
        Object[] args = new Object[]{wf_id,node_id};
        return JwDbHelper.queryFirst(WfNodeModel.class,sql,args);
    }

    @Override
    public WfNodeModel getPrevNode(String wf_id, String node_id) {
        String sql="select t1.* from jw_wf_node t1,jw_wf_link t2 where t1.wf_id=? and t1.wf_id=t2.wf_id " +
                " and t2.target_id=? and t1.id=t2.source_id ";
        Object[] args = new Object[]{wf_id,node_id};
        return JwDbHelper.queryFirst(WfNodeModel.class,sql,args);
    }

    @Override
    public List<WfNodeModel> getPrevNodes(String wf_id, String node_id) {
        List<WfNodeModel> list=new ArrayList<WfNodeModel>();
        return getPrevNode(list,wf_id,node_id);
    }

    /**
     * 递归处理查找多有上一步的节点，返回节点集合
     * @param list
     * @param wf_id
     * @param node_id
     * @return
     */
    public List<WfNodeModel> getPrevNode(List<WfNodeModel> list,String wf_id, String node_id)
    {
        String sql="select t1.* from jw_wf_node t1,jw_wf_link t2 where t1.wf_id=? and t1.wf_id=t2.wf_id " +
                " and t2.target_id=? and t1.id=t2.source_id ";
        Object[] args = new Object[]{wf_id,node_id};
        WfNodeModel wfNodeModel = JwDbHelper.queryFirst(WfNodeModel.class,sql,args);
        if(wfNodeModel==null)
        {
            return list;
        }
        list.add(wfNodeModel);
        return getPrevNode(list,wf_id, wfNodeModel.getId());
    }

    LinkService linkService=new LinkService();
    @Override
    public WfLinkModel getLink(String node_id)
    {
        return linkService.getPreLink(node_id,true);
    }
}
